package com.bra.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bra.R;
import com.bra.bean.VenueBean;
import com.bra.bean.HomeMessageBean;
import com.bra.lunbo.MainPage;
import com.bra.request.UrlConstants;
import com.bra.ui.activity.VenueDetailsActivity;
import com.bra.ui.activity.ArticleListActivity;
import com.bra.ui.adapter.Venue_type_adapter;
import com.bra.ui.adapter.VenueList_adapter;
import com.bra.ui.adapter.Home_Type_adapter;
import com.bra.ui.base.BaseActivity;
import com.bra.ui.base.BaseFragment;
import com.bra.ui.base.PlayFrameLayout;
import com.bra.utils.ImageUtil;
import com.bra.utils.SPHelper;
import com.easemob.easeui.ui.EaseBaiduMapActivity.MyLocationListenner;

 //场馆fragment
public class VenueFragment extends BaseFragment {
	private ListView cg_listview1, cg_listview2;
	private String[] items = new String[20];
	private VenueList_adapter adapter;
	private Venue_type_adapter adapter_item;
	private LinearLayout cg_play_sp, cg_address_sp, cg_huodong_sp,
			cg_pailie_sp,cg_layout;
	private TextView cg_play_tv,cg_title_name,home_title_name;
	private ImageView cg_play_im;
	private boolean cg_pd = false;
	private String PlayUrl = UrlConstants.IP;
	private RequestQueue mRequestQueue;
	private String Latitude,Longitude;
	private VenueBean  cgbean;
	private List<VenueBean>cgbeans;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return getRootView(R.layout.fragment_playq);
	}

	@Override
	protected void fillData() {

	}

	@Override
	protected void findView() {
		mRequestQueue = Volley.newRequestQueue(getActivity());
		Latitude=SPHelper.readString(getActivity(), "Latitude");
		Longitude=SPHelper.readString(getActivity(), "Longitude");	
		cg_title_name=(TextView) rootView.findViewById(R.id.cg_title_name);
		home_title_name=(TextView) rootView.findViewById(R.id.home_title_name);
		cg_title_name.setVisibility(View.VISIBLE);
		home_title_name.setVisibility(View.GONE);
		cg_listview1 = (ListView) rootView.findViewById(R.id.cg_listview1);
		cg_listview2 = (ListView) rootView.findViewById(R.id.cg_listview2);
		cg_play_sp = (LinearLayout) rootView.findViewById(R.id.cg_play_sp);
		cg_address_sp = (LinearLayout) rootView
				.findViewById(R.id.cg_address_sp);
		cg_huodong_sp = (LinearLayout) rootView
				.findViewById(R.id.cg_huodong_sp);
		cg_pailie_sp = (LinearLayout) rootView.findViewById(R.id.cg_pailie_sp);
		cg_play_tv = (TextView) rootView.findViewById(R.id.cg_play_tv);
		cg_play_im = (ImageView) rootView.findViewById(R.id.cg_play_im);
		cg_layout= (LinearLayout) rootView.findViewById(R.id.cg_layout);
		for (int i = 0; i < items.length; i++) {

			items[i] = "康比特运动城" + i;
		}
	
		if (adapter_item == null) {
			adapter_item = new Venue_type_adapter(getActivity(), items);
		}
		cg_click();
		httpRequestPlayInfo(getActivity(),PlayUrl,RequestParams(Latitude,Longitude));	
	}

	public void cg_click() {
		cg_play_sp.setOnClickListener(click);
		cg_listview1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Bundle bundle=new Bundle();
				bundle.putString("id", cgbeans.get(position).getId());
				bundle.putString("fraction", cgbeans.get(position).getFraction());
				bundle.putString("projectId", cgbeans.get(position).getProjectId());			
				((BaseActivity) getActivity()).goActivity(VenueDetailsActivity.class,bundle);				
			}
		});
		cg_listview2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
		//		cg_play_tv.setText(items[position]);

			}
		});
	}

	public View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.cg_play_sp:
				if (cg_pd) {
					cg_layout.setVisibility(View.GONE);
					cg_listview1.setEnabled(true);
					cg_listview1.setAlpha(1f);
					cg_play_im.setSelected(false);
					cg_play_tv.setTextColor(getResources().getColor(
							R.color.color_956803));
					cg_pd = false;
				} else {
					cg_layout.setVisibility(View.VISIBLE);
					cg_play_im.setSelected(true);
					cg_play_tv.setTextColor(getResources().getColor(
							R.color.color_e6e6e6));				
					cg_listview1.setAlpha(0.2f);
					cg_listview1.setEnabled(false);
					cg_listview2.setAdapter(adapter_item);
					cg_pd = true;
				}
				break;

			default:
				break;
			}

		}
	};
	//运动场馆的网络请求
	private void httpRequestPlayInfo(Context context, String url,
			final HashMap<String, String> params) {
	     	StringRequest req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						System.out.println(response.toString()+"=====");	
						cgbeans=new ArrayList<VenueBean>();
						try {
						   JSONObject object=new JSONObject(response.toString());
						   JSONArray data = object.getJSONArray("data");
						   for(int i=0;i<data.length();i++){
							  
							   JSONObject object_data=data.getJSONObject(i);
							   String  id	   =object_data.getString("id");
						       String  name	   =object_data.getString("name");
						       String  imgSrc	=object_data.getString("imgSrc");
						       String  distance	 =object_data.getString("distance");
						       String  price	=object_data.getString("price");
						       String  hot	 =object_data.getString("hot");
						       String  fraction	 =object_data.getString("fraction");
						       String projectId =object_data.getString("projectId");
						       cgbean=new VenueBean(id,name,imgSrc,distance,price,hot,fraction,projectId);
						       cgbeans.add(cgbean) ;
						   }    
							if (adapter == null) {
								adapter = new VenueList_adapter(getActivity(), cgbeans);
							}
							cg_listview1.setAdapter(adapter);
						   
						   return;
						} catch (JSONException e) {
						
							e.printStackTrace();
						}
						Toast.makeText(getActivity(), "数据异常，请检查网络", 0).show();	
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {
						try {
							Toast.makeText(getActivity(), "访问失败，请检查网络", 0).show();						
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;
				return map;
			}
		};
		mRequestQueue.add(req);
	}

	private HashMap<String, String> RequestParams(String Latitude,String Longitude) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userId", "");
		map.put("equipment", "android");
		map.put("requestType", "Venue_List_Api");
		map.put("token", "");
		map.put("lat", Longitude);
		map.put("lng", Latitude);
		return map;
	}	
}
